﻿Option Explicit On
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Diagnostics
Imports SpeechLib
Imports System.IO
Imports System.Data.OleDb




Public Class test
    
    Inherits Form
    'The Speech recognizer
    Dim recognizer As SpInprocRecognizer

    'The grammar that we're recognizing
    Dim grammar As ISpeechRecoGrammar

    'The filestream containing the speec
    Dim fileStream As SpFileStream

    'The recognizer context
    Dim WithEvents speechRecoContext As SpInProcRecoContext

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.Hide()
        apotelesmata.Show()
    End Sub
    Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
        ' record from microphone

        mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)

        mciSendString("record recsound", "", 0, 0)
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        ' stop  and save

        mciSendString("save recsound d:\record.wav", "", 0, 0)

        mciSendString("close recsound", "", 0, 0)

        Dim ofd As OpenFileDialog = New OpenFileDialog()

        'Restrict dialog to .WAV files
        ofd.DefaultExt = ".wav"
        ofd.Filter = "Wav files (.wav)|*.wav"
        'N.B.: 1-based index!
        ofd.FilterIndex = 1

        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            'Transcribe selected .WAV file
            TranscribeAudioFile(ofd.FileName)
        End If

    End Sub
    Sub TranscribeAudioFile(ByVal fName As String)
        'Create the recognizer
        recognizer = New SpInprocRecognizer()

        'Recognize the speec in the file that we passed in
        fileStream = New SpFileStream()
        fileStream.Open(fName, SpeechStreamFileMode.SSFMOpenForRead, True)
        recognizer.AudioInputStream = fileStream

        'Create a recognition context
        speechRecoContext = CType(recognizer.CreateRecoContext(), SpInProcRecoContext)

        'Create standard grammar
        grammar = speechRecoContext.CreateGrammar(10)
        Try
            'Begin recognition as soon as dictation (in this case, file playback) is begun 
            grammar.DictationSetState(SpeechRuleState.SGDSActive)
        Catch ce As System.Runtime.InteropServices.COMException
            Debug.WriteLine(ce.ToString())
        End Try
    End Sub
    'This will be called many times, as data is analyze
    Sub OnHypothesis(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal Result As ISpeechRecoResult) Handles speechRecoContext.Hypothesis
        SyncLock Me
            Dim info As ISpeechPhraseInfo = Result.PhraseInfo
            'You could, of course, store this for further processing / analysis
            Dim el As ISpeechPhraseElement
            For Each el In info.Elements
                Debug.WriteLine(el.DisplayText)
            Next
            Debug.WriteLine("--Hypothesis over--")
        End SyncLock
    End Sub

    'This will be called once, after entire file is analyzed
    Private Sub OnRecognition(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal RecognitionType As SpeechRecognitionType, ByVal Result As ISpeechRecoResult) Handles speechRecoContext.Recognition
        Dim phraseInfo As ISpeechPhraseInfo = Result.PhraseInfo
        'The best guess at the completely recognized text
        Dim s As String = phraseInfo.GetText(0, -1, True)
        result1.AppendText(s)

        'Or you could look at alternates. Here, request up to 10 alternates from index position 0 considering all elements (-1)
        Dim alternate As ISpeechPhraseAlternate
        For Each alternate In Result.Alternates(10, 10, -1)
            Dim altResult As ISpeechRecoResult = alternate.RecoResult
            Dim altInfo As ISpeechPhraseInfo = altResult.PhraseInfo
            Dim altString As String = altInfo.GetText(0, -1, True)
            Debug.WriteLine(altString)
        Next
    End Sub
    Private Sub OnAudioStreamEnd(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal someBool As Boolean) Handles speechRecoContext.EndStream
        'Stop recognition
        grammar.DictationSetState(SpeechRuleState.SGDSInactive)
        'Unload the active dictation topic
        grammar.DictationUnload()
    End Sub

    Private Sub test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label5.Hide()

        kseni.Text() = arxiki.Glwssa.SelectedItem

        Me.GreekTableAdapter.Fill(Me.LexikoDataSet.greek)

        If (arxiki.Glwssa.SelectedItem = "Ισπανικά") Then
            Me.SpanishTableAdapter.Fill(Me.LexikoDataSet.spanish)
            If (arxiki.Arxarios.Checked = True) Then
                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.easy words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "spanish.easy words")
            End If
            If (arxiki.Metrios.Checked = True) Then
                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.medium words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "spanish.medium words")
            End If
            If (arxiki.Proxorimenos.Checked = True) Then
                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.difficult words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "spanish.difficult words")
            End If
        End If
        If (arxiki.Glwssa.SelectedItem = "Αγγλικά") Then
            Me.EnglishTableAdapter.Fill(Me.LexikoDataSet.english)
            If (arxiki.Arxarios.Checked = True) Then

                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.easy words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "english.easy words")

            End If
            If (arxiki.Metrios.Checked = True) Then
                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.medium words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "english.medium words")
            End If
            If (arxiki.Proxorimenos.Checked = True) Then
                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.difficult words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "english.difficult words")
            End If
        End If
        If (arxiki.Glwssa.SelectedItem = "Ιταλικά") Then
            Me.ItalianTableAdapter.Fill(Me.LexikoDataSet.italian)
            If (arxiki.Arxarios.Checked = True) Then
                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.easy words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "italian.easy words")

            End If
            If (arxiki.Metrios.Checked = True) Then
                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.medium words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "italian.medium words")
            End If
            If (arxiki.Proxorimenos.Checked = True) Then
                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.difficult words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "italian.difficult words")
            End If
        End If
        If (arxiki.Glwssa.SelectedItem = "Γαλλικά") Then
            Me.FrenchTableAdapter.Fill(Me.LexikoDataSet.french)
            If (arxiki.Arxarios.Checked = True) Then
                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.easy words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "french.easy words")

            End If
            If (arxiki.Metrios.Checked = True) Then
                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.medium words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "french.medium words")
            End If
            If (arxiki.Proxorimenos.Checked = True) Then
                Label4.DataBindings.Add("Text", LexikoDataSet, "greek.difficult words")
                Label5.DataBindings.Add("Text", LexikoDataSet, "french.difficult words")
            End If
        End If

    End Sub

    Public grapto As Integer = 0
    Public proforiko As Integer = 0
    'den mpainei sthn if th deyteri fora
    'allagi text box se label
    Private Sub ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ok.Click
        If write.Text.Equals(Label5.Text) Then
            grapto = grapto + 10
            ' MsgBox(TextBox1.Text)
            'MsgBox(write.Text)
            'MsgBox(Label5.Text)
        End If
        If write.Text = Nothing Then
            grapto = grapto + 0
        End If
        If Not write.Text.Equals(Label5.Text) Then
            grapto = grapto + 10
        End If
        'MsgBox(TextBox1.Text)
        'MsgBox(write.Text)
        'MsgBox(Label5.Text)

    End Sub

    Private Sub Epomeni_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Epomeni.Click

        'gia ton midenismo oxi "" alla nothing

        write.Text = Nothing
        result1.Text = Nothing
        BindingContext(LexikoDataSet, "greek").Position = BindingContext(LexikoDataSet, "greek").Position + 1
        If (arxiki.Glwssa.SelectedItem = "Ισπανικά") Then
            BindingContext(LexikoDataSet, "spanish").Position = BindingContext(LexikoDataSet, "spanish").Position + 1
        End If
        If (arxiki.Glwssa.SelectedItem = "Αγγλικά") Then
            BindingContext(LexikoDataSet, "english").Position = BindingContext(LexikoDataSet, "english").Position + 1
        End If
        If (arxiki.Glwssa.SelectedItem = "Γαλλικά") Then
            BindingContext(LexikoDataSet, "french").Position = BindingContext(LexikoDataSet, "french").Position + 1
        End If
        If (arxiki.Glwssa.SelectedItem = "Ιταλικά") Then
            BindingContext(LexikoDataSet, "italian").Position = BindingContext(LexikoDataSet, "italian").Position + 1
        End If

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If result1.Text = Nothing Then
            proforiko = proforiko + 0
        ElseIf result1.Text.Equals(Label5) Then
            proforiko = proforiko + 10
        ElseIf Not result1.Text.Equals(Label5) Then
            proforiko = proforiko + 0
        End If
    End Sub

End Class